package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.UserApprove;
import com.woniu.mapper.UserApproveMapper;
import com.woniu.service.IUserApproveService;
import com.woniu.util.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hyl
 * @since 2022-08-28
 */
@Service
@Transactional
public class UserApproveServiceImpl extends ServiceImpl<UserApproveMapper, UserApprove> implements IUserApproveService {
    @Autowired
    private UserApproveMapper userApproveMapper;

    /*/**
     *@description 根据userID查询userApprove
     *@params [userId]
     *@return_value com.woniu.entity.UserApprove
     *@author hyl
     *@date 2022/8/30
     */
    @Override
    public UserApprove getUserAppByUserId(Integer userId) {
        return userApproveMapper.getUserAppByUserId(userId);
    }

    /*/**
     *@description 多条件查询userApprove
     *@params [userApprove]
     *@return_value java.util.List<com.woniu.entity.UserApprove>
     *@author hyl
     *@date 2022/8/30
     */
    @Override
    public List<UserApprove> getUserAppByAny(UserApprove userApprove) {
        return userApproveMapper.getUserAppByAny(getWrapper(userApprove));
    }

    /*/**
     *@description  条件判空
     *@params [userApprove]
     *@return_value com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
     *@author hyl
     *@date 2022/8/30
     */
    private static QueryWrapper getWrapper(UserApprove userApprove) {
        QueryWrapper<UserApprove> wrapper = new QueryWrapper<>();
        if (EmptyUtil.isEmpty(userApprove)) {
            wrapper.eq("1", "1");
            return wrapper;
        }
        if (EmptyUtil.isNoEmpty(userApprove.getUserApproveId())) {
            wrapper.eq("user_approve_id", userApprove.getUserApproveId());
        }
        if (EmptyUtil.isNoEmpty(userApprove.getUser()) && EmptyUtil.isNoEmpty(userApprove.getUser().getUserId())) {
            wrapper.eq("user_approve.user_id", userApprove.getUser().getUserId());
        }
        if (EmptyUtil.isNoEmpty(userApprove.getDept()) && EmptyUtil.isNoEmpty(userApprove.getDept().getDeptId())) {
            wrapper.eq("user_approve.dept_id", userApprove.getDept().getDeptId());
        }
        if (EmptyUtil.isNoEmpty(userApprove.getUserAppCategory()) && EmptyUtil.isNoEmpty(userApprove.getUserAppCategory().getUserAppCategoryId())) {
            wrapper.eq("user_approve.user_app_category_id", userApprove.getUserAppCategory().getUserAppCategoryId());
        }
        if (EmptyUtil.isNoEmpty(userApprove.getUserApproveStatus())) {
            wrapper.eq("user_approve_status", userApprove.getUserApproveStatus());
        }
        if (EmptyUtil.isNoEmpty(userApprove.getUserApplyDate())) {
            wrapper.eq("user_apply_date", userApprove.getUserApplyDate());
        }
        if (EmptyUtil.isNoEmpty(userApprove.getUserApproveDate())) {
            wrapper.eq("user_approve_date", userApprove.getUserApproveDate());
        }
        if (EmptyUtil.isNoEmpty(userApprove.getApplyDateSince())) {
            wrapper.gt("user_apply_date", userApprove.getApplyDateSince());
        }
        if (EmptyUtil.isNoEmpty(userApprove.getApplyDateEnd())) {
            wrapper.lt("user_apply_date", userApprove.getApplyDateEnd());
        }
        if (EmptyUtil.isNoEmpty(userApprove.getApproveDateSince())) {
            wrapper.gt("user_approve_date", userApprove.getApproveDateSince());
        }
        if (EmptyUtil.isNoEmpty(userApprove.getApproveDateEnd())) {
            wrapper.eq("user_approve_date", userApprove.getApproveDateEnd());
        }
        if (wrapper.isEmptyOfWhere()) {
            wrapper.eq("1", "1");
        }
        return wrapper;
    }

    /*/**
     *@description 多条件分页查询userApprove
     *@params [p, pageSize, userApprove]
     *@return_value com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.woniu.entity.UserApprove>
     *@author hyl
     *@date 2022/8/30
     */
    @Override
    public Page<UserApprove> getUserAppByAnyWithPage(Integer p, Integer pageSize, UserApprove userApprove) {
        QueryWrapper<UserApprove> wrapper = getWrapper(userApprove);
        Page<UserApprove> page = new Page<>(p, pageSize);
        return userApproveMapper.getUserAppByAnyWithPage(page, wrapper);
    }
}
